package com.bibrApp.difu.utils;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

public class UCaculateUtils {
    /**
     * 按规定进行位运算,强化数字
     *
     * @param num
     * @return
     */
    public static int changeNum(int num) {
        return (num | 0x80000000);
    }

    public static int changeNum(int num, boolean isHard) {
        return isHard ? (num | 0x80000000) : num;
    }

    /**
     * The path is a human-friendly representation of the deterministic path. For example:
     * <p>
     * "m/44'/0'/0'/0/0"
     * <p>
     * Where a letter "'" means hardened key. Spaces are ignored.
     */
    public static List<Integer> parsePath(@Nonnull String path) {
        String[] parsedNodes = path.replace("m", "").split("/");
        List<Integer> nodes = new ArrayList<>();

        for (String n : parsedNodes) {
            n = n.replaceAll(" ", "");
            if (n.length() == 0) continue;
            boolean isHard = n.endsWith("'");
            if (isHard) n = n.substring(0, n.length() - 1);
            int nodeNumber = Integer.parseInt(n);
            nodes.add(changeNum(nodeNumber, isHard));
        }
        return nodes;
    }
}
